<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>图上标绘 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,web-icons,bootstrap,bootstrap-checkbox,layer,toastr,haoutil,terraformer,turf,mars3d,mars3d-widget"
    ></script>

    <!-- KML/KMZ 转 GeoJSON 格式 -->
    <script type="text/javascript" src="../lib/kml/kml-geojson.js"></script>

    <link href="css/style.css" rel="stylesheet" />
    <style>
      .infoview div {
        margin-bottom: 5px;
      }
    </style>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <div class="infoview">
      <div>
        图层状态：
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkShow" class="styled" type="checkbox" checked />
          <label for="chkShow"> 显示隐藏 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkPopup" class="styled" type="checkbox" />
          <label for="chkPopup"> Popup绑定 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkTooltip" class="styled" type="checkbox" />
          <label for="chkTooltip"> Tooltip绑定 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkContextMenu" class="styled" type="checkbox" checked />
          <label for="chkContextMenu"> 右键菜单绑定 </label>
        </div>

        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkHasEdit" class="styled" type="checkbox" checked />
          <label for="chkHasEdit"> 是否编辑 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline" title="屏蔽拾取地形坐标，避免穿透3dtiles模型">
          <input id="chk_onlyPickModelPosition" class="styled" type="checkbox" />
          <label for="chk_onlyPickModelPosition"> 仅在3dtiles上标绘 </label>
        </div>
      </div>

      <div>
        图层管理：
        <input id="btnClear" type="button" class="btn btn-danger" value="清除" />
        <input id="btnImpFile" type="button" class="btn btn-primary" value="打开..." />

        <input id="btnExpFile" type="button" class="btn btn-warning" value="保存GeoJSON" title="保存为GeoJSON文件" />
        <input id="btnSaveKML" type="button" class="btn btn-warning" value="另存KML" />
        <input id="btnSaveWKT" type="button" class="btn btn-warning" value="另存WKT" />

        <input id="input_draw_file" type="file" accept=".json,.geojson,.kml,.kmz" style="display: none" />
      </div>

      <div>
        单个点类：
        <input type="button" class="btn btn-primary" value="点" onclick="drawPoint()" />
        <input type="button" class="btn btn-primary" value="图标点" onclick="drawMarker()" />
        <input type="button" class="btn btn-primary" value="文字" onclick="drawLabel()" />
        <input type="button" class="btn btn-primary" value="小模型" onclick="drawModel()" />
      </div>

      <div>
        二维空间：
        <input type="button" class="btn btn-primary" value="线" onclick="drawPolyline(false)" />
        <input type="button" class="btn btn-primary" value="曲线" onclick="drawCurve(false)" />
        <input type="button" class="btn btn-primary" value="走廊" onclick="drawCorridor(false)" />
        <input type="button" class="btn btn-primary" value="面" onclick="drawPolygon(false)" />
        <input type="button" class="btn btn-primary" value="圆" onclick="drawEllipse(false)" />
        <input type="button" class="btn btn-primary" value="矩形" onclick="drawRectangle(false)" />
        <input type="button" class="btn btn-primary" value="平面" onclick="draPlane()" />
      </div>

      <div>
        二维贴地：
        <input type="button" class="btn btn-primary" value="线" onclick="drawPolyline(true)" />
        <input type="button" class="btn btn-primary" value="曲线" onclick="drawCurve(true)" />
        <input type="button" class="btn btn-primary" value="走廊" onclick="drawCorridor(true)" />
        <input type="button" class="btn btn-primary" value="面" onclick="drawPolygon(true)" />
        <input type="button" class="btn btn-primary" value="圆" onclick="drawEllipse(true)" />
        <input type="button" class="btn btn-primary" value="矩形" onclick="drawRectangle(true)" />
      </div>

      <div>
        三维空间：
        <input type="button" class="btn btn-primary" value="墙" onclick="draWall(false)" />
        <input type="button" class="btn btn-primary" value="闭合墙" onclick="draWall(true)" />
        <input type="button" class="btn btn-primary" value="面立体" onclick="drawExtrudedPolygon()" />
        <input type="button" class="btn btn-primary" value="矩形立体" onclick="drawExtrudedRectangle()" />
        <input type="button" class="btn btn-primary" value="盒子" onclick="drawBox()" />
        <input type="button" class="btn btn-primary" value="圆柱" onclick="drawExtrudedCircle()" />
        <input type="button" class="btn btn-primary" value="圆锥" onclick="drawCylinder()" />
        <input type="button" class="btn btn-primary" value="球" onclick="drawEllipsoid()" />
      </div>
    </div>

    <!-- 切换视角到模型或地形（山区）的快捷按钮 -->
    <script type="text/javascript" src="js/center_terrain_3dtiles.js"></script>

    <!-- 图层管理相关处理js -->
    <script type="text/javascript" src="./js/graphicManager.js"></script>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;
      var graphicLayer;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          control: {
            infoBox: false,
          },
        });

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        graphicLayer = new mars3d.layer.GraphicLayer({
          hasEdit: true,
          isAutoEditing: true, //绘制完成后是否自动激活编辑
        });
        map.addLayer(graphicLayer);

        //图层管理的相关处理，代码在\js\graphicManager.js
        window.initLayerManager(graphicLayer);

        graphicLayer.unbindPopup();

        //绑定标绘相关事件监听(可以自行加相关代码实现业务需求，此处主要做示例)
        graphicLayer.on(mars3d.EventType.drawStart, function (e) {
          console.log("开始绘制", e);
        });
        graphicLayer.on(mars3d.EventType.drawAddPoint, function (e) {
          console.log("绘制过程中增加了点", e);
        });
        graphicLayer.on(mars3d.EventType.drawRemovePoint, function (e) {
          console.log("绘制过程中删除了点", e);
        });

        graphicLayer.on(mars3d.EventType.drawCreated, function (e) {
          console.log("创建完成", e);
        });
        graphicLayer.on(mars3d.EventType.editStart, function (e) {
          console.log("开始编辑", e);

          startEditing(e.graphic);
        });
        graphicLayer.on(mars3d.EventType.editMovePoint, function (e) {
          console.log("编辑修改了点", e);
          startEditing(e.graphic);
        });
        graphicLayer.on(mars3d.EventType.editRemovePoint, function (e) {
          console.log("编辑删除了点", e);

          startEditing(e.graphic);
        });
        graphicLayer.on(mars3d.EventType.editStop, function (e) {
          console.log("停止编辑", e);

          stopEditing();
        });
        graphicLayer.on(mars3d.EventType.removeGraphic, function (e) {
          console.log("删除了对象", e);

          stopEditing();
        });

        $("#chk_onlyPickModelPosition").change(function () {
          var val = $(this).is(":checked");

          //控制鼠标只取模型上的点，忽略地形上的点的拾取
          map.onlyPickModelPosition = val;
        });

        $("#btnSaveKML").click(function () {
          if (graphicLayer.length === 0) {
            window.layer.msg("当前没有标注任何数据，无需保存！");
            return;
          }
          var strResult = toKML();
          haoutil.file.downloadFile("我的标注.kml", strResult);
        });

        $("#btnSaveWKT").click(function () {
          if (graphicLayer.length === 0) {
            window.layer.msg("当前没有标注任何数据，无需保存！");
            return;
          }
          var strResult = toWKT();
          haoutil.file.downloadFile("我的标注wkt.txt", JSON.stringify(strResult));
        });

        if (location.hostname !== "127.0.0.1" && location.hostname !== "localhost") {
          queryDrawData().then(function (data) {
            graphicLayer.loadGeoJSON(data, { clear: true, flyTo: true });
          });
        }
      }

      //获取数据
      function queryDrawData() {
        //加载历史演示数据
        return new Promise(function (resolve, reject) {
          $.ajax({
            url: "//data.mars3d.cn/file/geojson/mars3d-draw.json",
            type: "get",
            dataType: "json",
            success: function (result) {
              resolve(result);
            },
            error: function (data) {
              reject(data);
            },
          });
        });
      }

      function drawPoint() {
        graphicLayer.startDraw({
          type: "point",
          style: {
            pixelSize: 12,
            color: "#3388ff",
            label: {
              //不需要文字时，去掉label配置即可
              text: "可以同时支持文字",
              font_size: 20,
              color: "#ffffff",
              outline: true,
              outlineColor: "#000000",
              pixelOffsetY: -20,
            },
          },
        });
      }

      function drawMarker() {
        graphicLayer.startDraw({
          type: "billboard",
          style: {
            image: "img/marker/mark1.png",
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            label: {
              //不需要文字时，去掉label配置即可
              text: "可以同时支持文字",
              font_size: 26,
              color: "#ffffff",
              outline: true,
              outlineColor: "#000000",
              pixelOffsetY: -60,
            },
          },
        });
      }

      function drawLabel() {
        graphicLayer.startDraw({
          type: "label",
          style: {
            text: "火星科技三维地球",
            color: "#0081c2",
            font_size: 50,
            outline: true,
            outlineColor: "#ffffff",
            outlineWidth: 2,
          },
        });
      }

      function drawModel() {
        graphicLayer.startDraw({
          type: "model",
          style: {
            scale: 10,
            url: "//data.mars3d.cn/gltf/mars/firedrill/xiaofangche.gltf",
          },
        });
      }

      function drawPolyline(clampToGround) {
        graphicLayer.startDraw({
          type: "polyline",
          // maxPointNum: 2,  //限定最大点数，可以绘制2个点的线，自动结束
          style: {
            color: clampToGround ? "#ffff00" : "#3388ff",
            width: 3,
            clampToGround: clampToGround,
          },
        });
      }

      function drawPolygon(clampToGround) {
        graphicLayer.startDraw({
          type: "polygon",
          style: {
            color: clampToGround ? "#ffff00" : "#3388ff",
            opacity: 0.5,
            outline: true,
            outlineColor: "#ffffff",
            outlineWidth: 2.0,
            clampToGround: clampToGround,
          },
        });
      }

      function drawCurve(clampToGround) {
        graphicLayer.startDraw({
          type: "curve",
          style: {
            color: clampToGround ? "#ffff00" : "#3388ff",
            width: 3,
            clampToGround: clampToGround,
          },
        });
      }

      function drawCorridor(clampToGround) {
        graphicLayer.startDraw({
          type: "corridor",
          style: {
            color: clampToGround ? "#ffff00" : "#3388ff",
            opacity: 0.6,
            width: 500,
            clampToGround: clampToGround,
          },
        });
      }

      function drawEllipse(clampToGround) {
        graphicLayer.startDraw({
          type: "circle",
          style: {
            color: clampToGround ? "#ffff00" : "#3388ff",
            opacity: 0.6,
            outline: true,
            outlineColor: "#ffffff",
            outlineWidth: 2.0,
            clampToGround: clampToGround,
          },
        });
      }

      function drawRectangle(clampToGround) {
        graphicLayer.startDraw({
          type: "rectangle",
          style: {
            color: clampToGround ? "#ffff00" : "#3388ff",
            opacity: 0.6,
            outline: true,
            outlineColor: "#ffffff",
            outlineWidth: 2.0,
            clampToGround: clampToGround,
          },
        });
      }

      function draPlane() {
        graphicLayer.startDraw({
          type: "plane",
          style: {
            color: "#00ff00",
            opacity: 0.8,
            plane_normal: "x",
            dimensions_x: 1000.0,
            dimensions_y: 1000.0,
          },
        });
      }

      function draWall(closure) {
        graphicLayer.startDraw({
          type: "wall",
          style: {
            color: "#00ff00",
            opacity: 0.8,
            diffHeight: 400,
            closure: closure, //是否闭合
          },
        });
      }

      function drawBox() {
        graphicLayer.startDraw({
          type: "box",
          style: {
            color: "#00ff00",
            opacity: 0.6,
            dimensions_x: 1000.0,
            dimensions_y: 1000.0,
            dimensions_z: 1000.0,
          },
        });
      }

      function drawCylinder() {
        graphicLayer.startDraw({
          type: "cylinder",
          style: {
            fill: true,
            color: "#00ff00",
            opacity: 0.6,
            length: 1000,
          },
        });
      }

      function drawEllipsoid() {
        graphicLayer.startDraw({
          type: "ellipsoid",
          style: {
            fill: true,
            color: "#00ff00",
            opacity: 0.6,
          },
        });
      }

      function drawExtrudedPolygon() {
        graphicLayer.startDraw({
          type: "polygon",
          style: {
            color: "#00ff00",
            opacity: 0.6,
            diffHeight: 300,
          },
        });
      }

      function drawExtrudedRectangle() {
        graphicLayer.startDraw({
          type: "rectangle",
          style: {
            color: "#00ff00",
            opacity: 0.6,
            diffHeight: 300,
          },
        });
      }

      function drawExtrudedCircle() {
        graphicLayer.startDraw({
          type: "circle",
          style: {
            color: "#00ff00",
            opacity: 0.6,
            diffHeight: 300,
          },
        });
      }

      function toKML() {
        var geojsonObject = graphicLayer.toGeoJSON();
        if (geojsonObject == null) {
          return null;
        }

        geojsonObject = haoutil.system.clone(geojsonObject);

        var kml = kgUtil.toKml(geojsonObject, {
          name: "Mars3D标绘数据",
          documentName: "Mars3D标绘数据文件",
          documentDescription: "标绘数据 by mars3d.cn",
          simplestyle: true,
        });

        return kml;
      }

      function toWKT() {
        var geojsonObject = graphicLayer.toGeoJSON();
        if (geojsonObject == null) {
          return null;
        }
        geojsonObject = haoutil.system.clone(geojsonObject);

        var arrWKT = [];
        var index = 0;
        geojsonObject.features.forEach((feature) => {
          var attr = feature.properties;
          var style = feature.properties.style;

          var wkt = Terraformer.WKT.convert(feature.geometry); //geojson转换WKT格式 ,terraformer库
          arrWKT.push({
            id: ++index,
            name: attr.name || "",
            remark: attr.remark || "",
            style: style,
            wkt: wkt,
          });
        });
        return arrWKT;
      }

      //附加：激活属性编辑widget【非必需，可以注释该方法内部代码】
      var timeTik;

      function startEditing(graphic) {
        clearTimeout(timeTik);

        var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
        if (plotAttr && plotAttr.isActivate) {
          plotAttr.startEditing(graphic, graphic.coordinates);
        } else {
          mars3d.widget.activate({
            map: map,
            uri: "widgets/plotAttr/widget.js",
            name: "属性编辑",
            graphic: graphic,
            lonlats: graphic.coordinates,
          });
        }
      }

      function stopEditing() {
        timeTik = setTimeout(function () {
          mars3d.widget.disable("widgets/plotAttr/widget.js");
        }, 200);
      }
      //附加：激活属性编辑widget【非必需，可以注释该方法内部代码】
    </script>
  </body>
</html>
